<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<title>许愿墙</title>
	<link rel="stylesheet" href="./css/style.css">
	<link rel="stylesheet" href="http://at.alicdn.com/t/font_3320593_tro5d7em9w.css">
</head>

<body>
	<!-- 头部 -->
	<div class="header-bar">
		<div class="w12">
			<span class="logo">许愿墙</span>
		</div>
	</div>
	<!-- 许愿墙 -->
	<div class="main">
		<!-- 发布按钮 -->
		<div class="title">
			<span class="add">我要许愿</span>
		</div>
		<!-- 心愿列表 -->
		<div class="list-box">

		</div>
		<!-- <div class="no-list">愿望墙是空的，快去添加</div> -->
	</div>

	<!-- 许愿表单 -->
	<div class="alert-box">
		<h4 class="tit">我要许愿</h4>
		<div class="row">
			<div class="col">
				<p>我的名字：</p>
				<input type="text" placeholder="不填写表示匿名">
			</div>
			<div class="col">
				<p>贴纸的颜色：</p>
				</p>
				<ul id="color">
					<li class="color-1" data-color="rgb(165, 248, 139)"></li>
					<li class="color-2" data-color="rgb(166, 227, 252)"></li>
					<li class="color-3" data-color="rgb(253, 251, 140)"></li>
					<li class="color-4" data-color="rgb(253, 196, 242)"></li>
				</ul>
			</div>
		</div>
		<div class="row-box">
			<p>我的愿望：</p>
			<div class="text-box">
				<textarea name="" placeholder="不得超过30字"></textarea>
				<span>0/80</span>
			</div>
		</div>
		<div class="btn-box">
			<span class="cancel">取消</span>
			<span class="ok">确定</span>
		</div>
	</div>
	<script src="../jquery.js"></script>
	<script src="../工具.js"></script>
	<script>
		getXuan()
		function getXuan() {
			$.ajax({
				method: 'get',
				url: 'http://124.223.14.236:3001/api/wish',
				success({ data }) {
					console.log(data);
					let n = data.list.map(item => {
						return `
			<div class="item" style="background-color: ${item.color};" data-collor:"${item.color}">
				<div class="tit" data-id="${item.id}">
					<span>From：<b>${item.username}</b></span>
					<span class="iconfont icon-shanchu" id="del" data-id="${item.id}"></span>
				</div>
				<div class="content">
					${item.content}
					<br />
					(${formTime(+item.time)})
				</div>
			</div>
			`
					})
					$('.list-box').html(n)
				}
			})
		}
		// 添加
		$('.add').on('click', function () {
			$('.alert-box').show()
		})
		$('#color').on('click', 'li', function () {
			let m = $(this).attr('data-color')
			console.log(m);
			localStorage.setItem('color', m)
		})
		// 添加愿望
		$('body').on('click', '.ok', function () {
			let p = JSON.stringify({
				content: $('.text-box textarea').val(),
				color: localStorage.getItem('color')
			})
			$('.alert-box').hide()
			$.ajax({
				method: 'post',
				url: 'http://124.223.14.236:3001/api/wish',
				headers: {
					Authorization: localStorage.getItem('token')
				},
				data: p,
				dataType: 'json',
				contentType: 'application/json',
				success(res) {
					getXuan()
				}
			})
		})
		// 点击取消
		$('body').on('click', '.cancel', function () {
			$('.alert-box').hide()
		})
		// 删除
		$('body').on('click', '#del', function () {
			let id = $(this).attr('data-id')
			$.ajax({
				method: 'DELETE',
				url: `http://124.223.14.236:3001/api/wish/${id}`,
				headers: {
					Authorization: localStorage.getItem('token')
				},
				success(res){
					getXuan()
				}

			})


// 修改
//  $('.list-box').on('click','.content',function(){
// 	let p=JSON.stringify({
// 	content:$(this).html(),
// 	color:$(this).parent().attr('data-collor')
// })
// 	let n=$('#idd').attr('data-id')
// 	console.log(n);
// 	$.ajax({
// 		method:'put',
// 		url:`http://124.223.14.236:3001/api/wish/${n}`,
// 		headers:{
// 			Authorization:localStorage.getItem('token')
// 		},
// 		data:p,
// 		dataType:'json',
// 		contentType:'application/json',
// 		success(res){
// 			console.log(res);
// 		}
// 	})
//  })

// 详情
// $('.list-box').on('click','.tit',function(){
// 	let n=$(this).attr('data-id')
// 	console.log(n);
// 	$.ajax({
// 		method:'GET',
// 		url:`http://124.223.14.236:3001/api/wish/${n}`,
// 		success(res){
// 			console.log(res);
// 		}
// 	})
})
	</script>
</body>

</html>